package com.ksyun.ks3.services.handler;

import com.ksyun.ks3.exception.Ks3ClientException;
import com.ksyun.ks3.exception.Ks3ServiceException;
import com.ksyun.ks3.model.HttpHeaders;
import com.ksyun.ks3.model.ObjectMetadata;
import com.ksyun.ks3.model.ObjectMetadata.Meta;
import com.ksyun.ks3.model.result.HeadObjectResult;
import com.ksyun.ks3.services.request.HeadObjectRequest;
import com.ksyun.ks3.services.request.Ks3HttpRequest;
import com.ksyun.ks3.util.DateUtil;
import com.ksyun.ks3.util.ResponseUtils;
import com.ksyun.ks3.util.StringUtils;

import okhttp3.Headers;

public abstract class HeadObjectResponseHandler extends Ks3HttpResponseHandler {

    public abstract void onFailure(HeadObjectRequest request, Ks3ClientException clientException, Ks3ServiceException serviceException);

    public abstract void onSuccess(HeadObjectRequest request, HeadObjectResult result);

    @Override
    public final void onSuccess(Ks3HttpRequest request, int statusCode, Headers responseHeaders, byte[] response) {
        HeadObjectResult result = parseHeaders(responseHeaders);
        result.setStatusCode(statusCode);
        result.setHeaders(ResponseUtils.parseResponseHeaders(responseHeaders));
        this.onSuccess((HeadObjectRequest) request, result);
    }

    @Override
    public final void onFailure(Ks3HttpRequest request, Ks3ClientException clientException, Ks3ServiceException serviceException) {
        this.onFailure((HeadObjectRequest) request, clientException, serviceException);
    }

    @Override
    public final void onProgress(long bytesWritten, long totalSize) {
    }

    @Override
    public final void onStart() {
    }

    @Override
    public final void onFinish() {
    }

    @Override
    public final void onCancel() {
    }

    private HeadObjectResult parseHeaders(Headers responseHeaders) {

        HeadObjectResult result = new HeadObjectResult();
        ObjectMetadata meta = new ObjectMetadata();
        try {
            Headers header = responseHeaders;
            for (int i = 0; i < header.size(); i++) {
                String name = header.name(i);
                String value = header.value(i);
                if (HttpHeaders.ETag.toString().equalsIgnoreCase(name)) {
                    result.setETag(value);
                }
                if (HttpHeaders.LastModified.toString().equalsIgnoreCase(name)) {
                    String dateStr = value;
                    if (!StringUtils.isBlank(dateStr))
                        result.setLastmodified(DateUtil.ConverToDate(dateStr));
                }
                if (name.startsWith(ObjectMetadata.userMetaPrefix)) {
                    meta.addOrEditUserMeta(name, value);
                } else if (name.equalsIgnoreCase(Meta.CacheControl.toString())) {
                    meta.setCacheControl(value);
                } else if (name.equalsIgnoreCase(Meta.ContentDisposition.toString())) {
                    meta.setContentDisposition(value);
                } else if (name.equalsIgnoreCase(Meta.ContentEncoding.toString())) {
                    meta.setContentEncoding(value);
                } else if (name.equalsIgnoreCase(Meta.ContentLength.toString())) {
                    meta.setContentLength(value);
                } else if (name.equalsIgnoreCase(Meta.ContentType.toString())) {
                    meta.setContentType(value);
                } else if (name.equalsIgnoreCase(Meta.Expires.toString())) {
                    meta.setExpires(value);
                } else if (name.equalsIgnoreCase(Meta.XKssObjectTagCount.toString())) {
                    meta.setObjectTagCount(value);
                }
                result.setObjectMetadata(meta);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }

        return result;
    }

}
